<template>
	<el-drawer :title="titleMap[mode]" v-model="visible" :size="600" append-to-body destroy-on-close @closed="$emit('closed')">
		<el-container>
			<el-main class="nopadding">
				<el-scrollbar>
					<el-row class="drawer-table">
						<el-col :span="24">
							<el-form :model="form" :rules="rules" :disabled="mode==='show'" ref="dialogForm" label-width="80px">
								<el-form-item label="个人收件地址" prop="customerAddressId">
									<el-input v-model="form.customerAddressId" placeholder="请输入个人收件地址" clearable></el-input>
								</el-form-item>
								<el-form-item label="会员" prop="customerId">
									<el-input v-model="form.customerId" placeholder="请输入会员" clearable></el-input>
								</el-form-item>
								<el-form-item label="部门/分店" prop="departmentId">
									<el-input v-model="form.departmentId" placeholder="请输入部门/分店" clearable></el-input>
								</el-form-item>
								<el-form-item label="省份" prop="province">
									<el-input v-model="form.province" placeholder="请输入省份" clearable></el-input>
								</el-form-item>
								<el-form-item label="城市" prop="city">
									<el-input v-model="form.city" placeholder="请输入城市" clearable></el-input>
								</el-form-item>
								<el-form-item label="片区 商圈" prop="district">
									<el-input v-model="form.district" placeholder="请输入片区 商圈" clearable></el-input>
								</el-form-item>
								<el-form-item label="自己输入门牌号" prop="doorNumber">
									<el-input v-model="form.doorNumber" placeholder="请输入自己输入门牌号" clearable></el-input>
								</el-form-item>
								<el-form-item label="路径" prop="regionPath">
									<el-input v-model="form.regionPath" placeholder="请输入路径" clearable></el-input>
								</el-form-item>
								<el-form-item label="详细地址" prop="detailed">
									<el-input v-model="form.detailed" placeholder="请输入详细地址" clearable></el-input>
								</el-form-item>
								<el-form-item label="联系人" prop="recName">
									<el-input v-model="form.recName" placeholder="请输入联系人" clearable></el-input>
								</el-form-item>
								<el-form-item label="联系电话" prop="recTel">
									<el-input v-model="form.recTel" placeholder="请输入联系电话" clearable></el-input>
								</el-form-item>
								<el-form-item label="邮编" prop="zip">
									<el-input v-model="form.zip" placeholder="请输入邮编" clearable></el-input>
								</el-form-item>
								<el-form-item label="邮箱" prop="email">
									<el-input v-model="form.email" placeholder="请输入邮箱" clearable></el-input>
								</el-form-item>
								<el-form-item label="经度 配送地址" prop="longitude">
									<el-input v-model="form.longitude" placeholder="请输入经度 配送地址" clearable></el-input>
								</el-form-item>
								<el-form-item label="纬度 配送地址" prop="latitude">
									<el-input v-model="form.latitude" placeholder="请输入纬度 配送地址" clearable></el-input>
								</el-form-item>
								<el-form-item label="是否默认地址" prop="isDefault">
									<el-input v-model="form.isDefault" placeholder="请输入是否默认地址" clearable></el-input>
								</el-form-item>
								<el-form-item label="1家 2公司 3学校" prop="dtype">
									<el-input v-model="form.dtype" placeholder="请输入1家 2公司 3学校" clearable></el-input>
								</el-form-item>
                            </el-form>
						</el-col>
					</el-row>
				</el-scrollbar>
			</el-main>
			<el-footer>
				<el-button v-if="mode!=='show'" :loading="isSaveing" @click="submit()" type="primary" size="small">保 存</el-button>
				<el-button size="small" @click="visible=false">取 消</el-button>
			</el-footer>
		</el-container>
	</el-drawer>
</template>
<script>
	import customerAPI from '@/api/customer/customerAddress';
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增',
					edit: '编辑',
					mode: ''
				},
				visible: false,
				isSaveing: false,
				//表单数据
				form: {
					customerAddressId: "",
					customerId: "",
					departmentId: "",
					province: "",
					city: "",
					district: "",
					doorNumber: "",
					regionPath: "",
					detailed: "",
					recName: "",
					recTel: "",
					zip: "",
					email: "",
					longitude: "",
					latitude: "",
					isDefault: "",
					dtype: "",
				},
				//验证规则
				rules: {
					userName: [
						{required: true, message: '请输入名称'}
					],
					userAge: [
						{pattern: /^[0-9]*$/, message: '年龄需为数字', trigger: 'blur'}
					],
					price: [
						{pattern: /^\d+(\.\d+)?$/, message: '价格需为数字', trigger: 'blur'}
					]
				}
			}
		},
		mounted() {

		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this;
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if(valid){
						this.isSaveing = true;
						var res = await customerAPI.customerAddress.save.post(this.form);
						this.isSaveing = false;
						if(res.code === 2000){
							this.$emit('success', this.form, this.mode);
							this.visible = false;
							this.$message.success(res.message);
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}
				});
			},
			//表单注入数据
			setData(data){
				this.form.customerAddressId = data.customerAddressId;
				this.form.customerId = data.customerId;
				this.form.departmentId = data.departmentId;
				this.form.province = data.province;
				this.form.city = data.city;
				this.form.district = data.district;
				this.form.doorNumber = data.doorNumber;
				this.form.regionPath = data.regionPath;
				this.form.detailed = data.detailed;
				this.form.recName = data.recName;
				this.form.recTel = data.recTel;
				this.form.zip = data.zip;
				this.form.email = data.email;
				this.form.longitude = data.longitude;
				this.form.latitude = data.latitude;
				this.form.isDefault = data.isDefault;
				this.form.dtype = data.dtype;
			}
		}
	}
</script>
<style>
</style>
